package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入删除的图书的名字：");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        int index=-1;
        int curSize=bookList.getUsedSize();
        for (int i = 0; i < curSize; i++) {
            Book book=bookList.getPos(i);
            if(name.equals(book.getName())){
                index=i;
                break;
            }
        }
        if(index==-1) {
            System.out.println("没有要删除的书");
            return;
        }
        for (int i = 0; i < curSize-1; i++) {
            Book book=bookList.getPos(i+1);
            bookList.setBooks(book,i);

        }
        bookList.setBooks(null,curSize-1);
        bookList.setUsedSize(curSize-1);
        System.out.println("删除成功");
    }
}
